package com.bigdata.service.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.bigdata.common.utils.StringUtils;
import com.bigdata.common.core.page.TableDataInfo;
import com.bigdata.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.bigdata.service.domain.bo.PointTransactionsBo;
import com.bigdata.service.domain.vo.PointTransactionsVo;
import com.bigdata.service.domain.PointTransactions;
import com.bigdata.service.mapper.PointTransactionsMapper;
import com.bigdata.service.service.IPointTransactionsService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 积分交易明细Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-15
 */
@RequiredArgsConstructor
@Service
public class PointTransactionsServiceImpl implements IPointTransactionsService {

    private final PointTransactionsMapper baseMapper;

    /**
     * 查询积分交易明细
     */
    @Override
    public PointTransactionsVo queryById(String transactionId){
        return baseMapper.selectVoById(transactionId);
    }

    /**
     * 查询积分交易明细列表
     */
    @Override
    public TableDataInfo<PointTransactionsVo> queryPageList(PointTransactionsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PointTransactions> lqw = buildQueryWrapper(bo);
        Page<PointTransactionsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询积分交易明细列表
     */
    @Override
    public List<PointTransactionsVo> queryList(PointTransactionsBo bo) {
        LambdaQueryWrapper<PointTransactions> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PointTransactions> buildQueryWrapper(PointTransactionsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PointTransactions> lqw = Wrappers.lambdaQuery();
        lqw.eq(null!=bo.getUserId(), PointTransactions::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getRuleId()), PointTransactions::getRuleId, bo.getRuleId());
        lqw.eq(StringUtils.isNotBlank(bo.getDatasetId()), PointTransactions::getDatasetId, bo.getDatasetId());
        lqw.eq(bo.getPoints() != null, PointTransactions::getPoints, bo.getPoints());
        lqw.eq(StringUtils.isNotBlank(bo.getTransactionType()), PointTransactions::getTransactionType, bo.getTransactionType());
        lqw.eq(StringUtils.isNotBlank(bo.getRemarks()), PointTransactions::getRemarks, bo.getRemarks());
        lqw.orderByDesc(PointTransactions::getCreateTime);
        lqw.like(StringUtils.isNotBlank(bo.getDatasetName()), PointTransactions::getDatasetName, bo.getDatasetName());
        lqw.like(StringUtils.isNotBlank(bo.getCreateUserName()), PointTransactions::getCreateUserName, bo.getCreateUserName());
        lqw.orderByDesc(PointTransactions::getCreateTime);
        return lqw;
    }

    /**
     * 新增积分交易明细
     */
    @Override
    public Boolean insertByBo(PointTransactionsBo bo) {
        PointTransactions add = BeanUtil.toBean(bo, PointTransactions.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setTransactionId(add.getTransactionId());
        }
        return flag;
    }

    /**
     * 修改积分交易明细
     */
    @Override
    public Boolean updateByBo(PointTransactionsBo bo) {
        PointTransactions update = BeanUtil.toBean(bo, PointTransactions.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PointTransactions entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除积分交易明细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
